﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace EssenceFeed.Core.Helpers
{
    /// <summary>
    /// Date Time Extensions
    /// </summary>
    public static class DateTimeExtensions
    {
        private static readonly string[] Months =
            {
                "January", "February", "March", "April", "May",
                "June", "July", "August", "September", "October",
                "November", "December"
            };

        private static readonly string[] Days = {
                                                    "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday",
                                                    "Sunday"
                                                };

        private static IEnumerable<SelectListItem> getMonthListItems(int? selectedMonth)
        {
            var monthList = new List<SelectListItem>();

            for (int i = 0; i < Months.Length; i++)
            {
                monthList.Add(new SelectListItem
                                  {
                                      Text = Months[i],
                                      Value = (i + 1).ToString(),
                                      Selected = (selectedMonth == (i + 1))
                                  });
            }

            return monthList;
        }

        private static IEnumerable<SelectListItem> getDaysListItems()
        {
            var dayList = new List<SelectListItem>();

            for (int i = 0; i < Days.Length; i++)
            {
                dayList.Add(new SelectListItem
                {
                    Text = Days[i],
                    //Value = (i + 1).ToString(),
                    Value = Days[i]
                });
            }

            return dayList;
        }


        private static IEnumerable<SelectListItem> getYearListItems(int? selectedYear)
        {
            var yearList = new List<SelectListItem>();

            for (int i = 2006; i <= DateTime.Now.Year; i++)
            {
                yearList.Add(new SelectListItem
                                 {
                                     Text = i.ToString(),
                                     Value = i.ToString(),
                                     Selected = (selectedYear == i)
                                 });
            }

            return yearList;
        }

        /// <summary>
        /// MonthDropDownList
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="expression"></param>
        /// <param name="selectedMonth"></param>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TProperty"></typeparam>
        /// <returns></returns>
        public static MvcHtmlString MonthDropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,
                                                                            Expression<Func<TModel, TProperty>>
                                                                                expression, int? selectedMonth)
        {
            return htmlHelper.DropDownListFor(expression, getMonthListItems(selectedMonth));
        }

        /// <summary>
        /// MonthDropDownList
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="expression"></param>
        /// <param name="selectedDay"></param>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TProperty"></typeparam>
        /// <returns></returns>
        public static MvcHtmlString DayDropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,
                                                                            Expression<Func<TModel, TProperty>>
                                                                                expression, int? selectedDay)
        {
            return htmlHelper.DropDownListFor(expression, getDaysListItems());
        }


        /// <summary>
        /// YearDropDownList
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="expression"></param>
        /// <param name="selectedYear"></param>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TProperty"></typeparam>
        /// <returns></returns>
        public static MvcHtmlString YearDropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,
                                                                           Expression<Func<TModel, TProperty>>
                                                                               expression, int? selectedYear)
        {
            return htmlHelper.DropDownListFor(expression, getYearListItems(selectedYear));
        }

        /// <summary>
        /// This is a method in my Helper Class. This method get the Id and DisplayName from the .net TimeZoneInfo
        /// </summary>
        /// <returns></returns>
        public static Dictionary<string, string> GetTimeZones()
        {
            return TimeZoneInfo.GetSystemTimeZones().ToDictionary(tz => tz.Id, tz => tz.DisplayName);
        }

        /// <summary>
        /// Convert to SqlDateTime
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static DateTime ToSqlDateTime(this DateTime dt)
        {
            return new DateTime(dt.Ticks - (dt.Ticks%10000000));
        }

        /// <summary>
        /// Convert Date to String
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static string ToStringForEdit(this DateTime dateTime)
        {
            return string.Format("{0} {1}", dateTime.ToShortDateString(), dateTime.ToShortTimeString());
        }

        /// <summary>
        /// Convert Date time
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static string ToStringForFeed(this DateTime dateTime)
        {
            return dateTime.ToString("R");
        }
    }
}